#include "stdafx.h"
#include "FCControl.h"
#include "..//MainFrame.h"
#include "GdiPaint.h"

namespace DuiLib
{
	FCControl::FCControl()
	{
		m_host = 0;
		m_native = 0;
	}

	FCControl::~FCControl(){
		if(m_native){
			delete m_native;
			m_native = 0;
		}
		m_host = 0;
		m_native = 0;
	}

	bool FCControl::Contains(RECT rect,POINT pt)
	{
		if(pt.x>=rect.left && pt.x<=rect.right
			&& pt.y>=rect.top && pt.y<=rect.bottom)
		{
			return true;
		}
		return false;
	}

	RECT FCControl::CopyRect(RECT copyRect)
	{
		RECT rect;
		rect.left=copyRect.left;
		rect.right=copyRect.right;
		rect.top=copyRect.top;
		rect.bottom=copyRect.bottom;
		return rect;
	}

	RECT FCControl::DisplayRect()
	{
		RECT rect;
		rect.left=this->GetX();
		rect.top=this->GetY();
		rect.right=this->GetWidth() + rect.left - 1;
		rect.bottom=this->GetHeight() + rect.top - 1;
		return rect;
	}

	void FCControl::DoEvent(TEventUI& event)
	{
		if( event.Type == UIEVENT_BUTTONDOWN)
		{
			DoMouseDown(1);
		}
		else if(event.Type==UIEVENT_DBLCLICK)
		{
			DoMouseDown(2);
		}
		else if(event.Type==UIEVENT_MOUSEMOVE)
		{
			DoMouseMove();
		}
		else if(event.Type==UIEVENT_BUTTONUP)
		{
			DoMouseUp();
		}
		else if(event.Type==UIEVENT_TIMER)
		{
			DoTimer();
		}
	}

	void FCControl::DoMouseDown(int clicks)
	{
		POINT pt = { 0 };
        ::GetCursorPos(&pt);
        ::ScreenToClient(m_pManager->GetPaintWindow(), &pt);
		pt.x -= this->GetX();
		pt.y -= this->GetY();
		m_host->setTouchPoint(pt);
		FCTouchInfo touchInfo;
		touchInfo.m_clicks = clicks;
		touchInfo.m_firstTouch = true;
		touchInfo.m_firstPoint = pt;
		m_native->onTouchDown(touchInfo);
		this->NeedParentUpdate();
	}

	void FCControl::DoMouseMove()
	{
		POINT pt = { 0 };
        ::GetCursorPos(&pt);
        ::ScreenToClient(m_pManager->GetPaintWindow(), &pt);
		pt.x -= this->GetX();
		pt.y -= this->GetY();
		m_host->setTouchPoint(pt);
		FCTouchInfo touchInfo;
		if(m_native->m_touchDownView){
			touchInfo.m_clicks = 1;
			touchInfo.m_firstTouch = true;
		}
		touchInfo.m_firstPoint = pt;
		m_native->onTouchMove(touchInfo);
		this->NeedParentUpdate();
	}

	void FCControl::DoMouseUp()
	{
		POINT pt = { 0 };
        ::GetCursorPos(&pt);
        ::ScreenToClient(m_pManager->GetPaintWindow(), &pt);
		pt.x -= this->GetX();
		pt.y -= this->GetY();
		m_host->setTouchPoint(pt);
		FCTouchInfo touchInfo;
		touchInfo.m_clicks = 1;
		touchInfo.m_firstTouch = true;
		touchInfo.m_firstPoint = pt;
		m_native->onTouchUp(touchInfo);
		this->NeedParentUpdate();
	}

	void FCControl::DoTimer()
	{
		if(m_host){
			m_host->onTimer();
		}
	}

	void FCControl::DoPaint(HDC hDC, const RECT& rcPaint){
		OnLoad();
		FCSize dSize = {this->GetWidth(), this->GetHeight()};
		if(m_native->getSize().cx != dSize.cx || m_native->getSize().cy != dSize.cy){
			m_native->setSize(dSize);
			m_native->update();
		}
		GdiPaint *dPaint = (GdiPaint*)m_native->getPaint();
		dPaint->m_offsetX2 = this->GetX();
		dPaint->m_offsetY2 = this->GetY();
		dPaint->setScaleFactor(1, 1);
		FCRect clipRect = {0, 0, dSize.cx, dSize.cy};
		dPaint->beginPaint(hDC, clipRect, clipRect);
		m_native->onPaint(clipRect);
		dPaint->endPaint();
	}

	void FCControl::OnLoad(){
		if(!m_native){
			m_native = new FCNative;
			String appPath = FCTran::stringToString(FCTran::getAppPath());
			m_native->setResourcePath(appPath + L"\\");
			m_native->setPaint(new GdiPaint());
			WinHost *host = new WinHost;
			host->setNative(m_native);
			m_native->setHost(host);
			host->m_view = this;
			m_host = host;
			FCSize dSize = {this->GetWidth(), this->GetHeight()};
			m_native->setSize(dSize);

			MainFrame *mainFrame = new MainFrame;
			mainFrame->setNative(m_native);
			mainFrame->loadFile(appPath + L"\\config\\MainFrame.xml", 0);
			m_native->update();
			m_native->invalidate();
			m_pManager->SetTimer(this, 0, 10);
		}
	}
}